package org.cug.controller;

import lombok.extern.slf4j.Slf4j;
import org.cug.pojo.GraduateTeaching;
import org.cug.pojo.Result;
import org.cug.service.GraduateTeachingService;
import org.cug.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/graduateTeaching")
@Slf4j
/**
 * 该控制器为对研究室授课进行操作，操作的数据库为graduate_teaching
 * state:已测验
 */
public class GraduateTeachingController {
    @Autowired
    private GraduateTeachingService graduateService;
    @GetMapping
    public Result list(@RequestBody(required = false) Short semester){
        log.info("查询所有研究生教学信息");
        //本地线程获取
        Map<String,Object> claims= ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        List<GraduateTeaching> graduateTeachings =graduateService.list(userId,semester);
        return Result.success(graduateTeachings);
    }

    @PostMapping
    public Result add(@RequestBody GraduateTeaching graduateTeaching){
        log.info("增加研究生教学信息{}", graduateTeaching);
        Map<String,Object> claims= ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        graduateTeaching.setUserId(userId);
        graduateService.add(graduateTeaching);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除研究生教学信息{}",ids);
        graduateService.delete(ids);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectid(@PathVariable Integer id) {
        log.info("查询id{}",id);
        GraduateTeaching graduateTeaching =graduateService.selectid(id);
        return Result.success(graduateTeaching);
    }

    @PutMapping
    public Result alterbyid(@RequestBody GraduateTeaching graduateTeaching){
        log.info("修改研究生教学信息{}", graduateTeaching);
        graduateService.alterbyid(graduateTeaching);
        return Result.success();
    }
}
